/* -*- tab-width: 4 -*-
 *
 * Electric(tm) VLSI Design System
 *
 * File: BitmapTraverserHandler.java
 * Written by: Daniel Schmidt
 * 
 * This code has been developed at the Karlsruhe Institute of Technology (KIT), Germany, 
 * as part of the course "Multicore Programming in Practice: Tools, Models, and Languages".
 * Contact instructor: Dr. Victor Pankratius (pankratius@ipd.uka.de)
 *
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 *
 * Electric(tm) is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Electric(tm) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Electric(tm); see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, Mass 02111-1307, USA.
 */
package com.sun.electric.plugins.minarea.mk01.bitmapTraverser;

import java.util.BitSet;
import java.util.Collection;

import com.sun.electric.api.minarea.ErrorLogger;
import com.sun.electric.plugins.minarea.mk01.MinAreaCheckerImplementation;
import com.sun.electric.plugins.minarea.mk01.MinAreaCheckerTask;
import com.sun.electric.plugins.minarea.mk01.datatypes.MinAreaPolygon;

public class BitmapTraverserHandler {

	private BitSet[] bitMap;
	private int[] indexToXCoordinate;
	private int[] indexToYCoordinate;
	private int xsize;
	private ErrorLogger errorLogger;
	private long minArea;
	
	/**
	 * C'tor
	 * 
	 * @param bitMap
	 * @param indexToXCoordinate
	 * @param indexToYCoordinate
	 * @param xsize
	 * @param errorLogger
	 * @param minArea
	 */
	public BitmapTraverserHandler(BitSet[] bitMap, int[] indexToXCoordinate, int[] indexToYCoordinate,
			int xsize, ErrorLogger errorLogger, long minArea) {
		this.bitMap = bitMap;
		this.indexToXCoordinate = indexToXCoordinate;
		this.indexToYCoordinate = indexToYCoordinate;
		this.xsize = xsize;
		this.errorLogger = errorLogger;
		this.minArea = minArea;
	}
	
	/**
	 * Initialize the THRESHOLD for each task, create the first one and start the
	 * execution of phase 3.
	 */
	public void start()  {

		int columns = Math.min(MinAreaCheckerImplementation.numOfThreads, bitMap.length);
	    BitmapTraverserTask.THRESHOLD = xsize / columns;
	    
		BitmapTraverserTask bitmapTraverser = new BitmapTraverserTask(bitMap, 
		          indexToXCoordinate, indexToYCoordinate, 0, xsize - 1, errorLogger, minArea);
		Collection<MinAreaPolygon> violations = MinAreaCheckerTask.getForkJoinPool().invoke(bitmapTraverser);
		
		//Check whether the size of those polygons, that range over the whole 
		//bitmap, is less than the specified minimum area. If so, report the
		//violation. All other polygons have already been checked during traversal.
		for(MinAreaPolygon polygon : violations) {
			if(polygon.getArea() < minArea) {
				errorLogger.reportMinAreaViolation(
						polygon.getArea(), 
						indexToXCoordinate[polygon.getUpperRight().getX() + 1], 
						indexToXCoordinate[polygon.getUpperRight().getY() + 1], 
						null);
			}
		}
	}
}
